package com.bdcom.component;

import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.bdcom.MainFrame;

public class StatusBar extends JPanel{
	
	private JTextField xField = null;
	private JTextField yField = null;
	private JTextField wField = null;
	private JTextField hField = null;
	private	JButton okBtn = null;
	private DragableImgLabel imgLabel = null;
	private MainFrame frame = null;
	
	private JPanel gp = null;
	
	private boolean canuse = false;
	
	public StatusBar(MainFrame mf ){
		this.setPreferredSize(new Dimension(0,30));
		this.setLayout(null);
		this.frame = mf;
		
		JLabel label1 = new JLabel("x:");
		JLabel label2 = new JLabel("y:");
		JLabel label3 = new JLabel("w:");
		JLabel label4 = new JLabel("h:");
		xField = new JTextField(5);
		yField = new JTextField(5);
		wField = new JTextField(5);
		hField = new JTextField(5);
		okBtn = new JButton("ok");
		
		
		this.add(label1);
		this.add(xField);
		this.add(label2);
		this.add(yField);
		this.add(label3);
		this.add(wField);
		this.add(label4);
		this.add(hField);
		this.add(okBtn);
		label1.setBounds(10, 5, 15, 20);
		xField.setBounds(25, 8, 30, 20);
		
		label2.setBounds(60, 5, 15, 20);
		yField.setBounds(75, 8, 30, 20);
		
		label3.setBounds(110, 5, 15, 20);
		wField.setBounds(125, 8, 30, 20);
		
		label4.setBounds(160, 5, 15, 20);
		hField.setBounds(175, 8, 30, 20);
		okBtn.setBounds(210, 5, 50, 25);
		okBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent actionevent) {
//				System.out.println("OKBTN");
				if(imgLabel!=null){
					int xValue = getXValue();
					int yValue = getYValue();
					int wValue = getWValue();
					int hValue = getHValue();
					imgLabel.setBounds(xValue,yValue,wValue,hValue);
					imgLabel.repaint();
					frame.requestFocusForMain();
				}
			}
		});
	}
	
	public void setDisable(boolean flag){
		if(canuse == flag){
			return ;
		}
		if(flag){
			if(gp == null){
				gp = createGlassPane();
			}
			setStatus(-1,-1,-1,-1);
			this.add(gp,0);
			gp.setBounds(0, 0, 500, 300);
		}else{
			this.remove(gp);
		}
		canuse = flag;
		this.repaint();
	}
	
	private JPanel createGlassPane(){
		JPanel tmp = new JPanel(){
			@Override
			protected void paintComponent(Graphics g) {
				Graphics2D g2d = (Graphics2D) g.create();
				AlphaComposite newComposite = AlphaComposite.getInstance(
						AlphaComposite.SRC_OVER, 0.3f);
				g2d.setComposite(newComposite);
				g2d.setColor(Color.LIGHT_GRAY);
				g2d.fillRect(0, 0,300,30);
			}
		};
		
		tmp.addMouseListener(new MouseListener() {
			public void mouseReleased(MouseEvent arg0) {}
			public void mousePressed(MouseEvent arg0) {}
			public void mouseExited(MouseEvent arg0) {}
			public void mouseEntered(MouseEvent arg0) {}
			public void mouseClicked(MouseEvent arg0) {}
		});
		tmp.setOpaque(false);
		tmp.setLayout(new BorderLayout());
		tmp.setPreferredSize(this.getPreferredSize());
		return tmp;
	}

	public JButton getOkBtn() {
		return okBtn;
	}
	
	public int getXValue(){
		try {
			return Integer.parseInt(xField.getText().trim());
		} catch (NumberFormatException e) {
			return -1;
		}
	}
	
	public int getYValue(){
		try {
			return Integer.parseInt(yField.getText().trim());
		} catch (NumberFormatException e) {
			return -1;
		}
	}
	
	public int getWValue(){
		try {
			return Integer.parseInt(wField.getText().trim());
		} catch (NumberFormatException e) {
			return -1;
		}
	}
	
	public int getHValue(){
		try {
			return Integer.parseInt(hField.getText().trim());
		} catch (NumberFormatException e) {
			return -1;
		}
	}
	
	public void setStatus(int x,int y,int w,int h){
		xField.setText(x+"");
		yField.setText(y+"");
		wField.setText(w+"");
		hField.setText(h+"");
	}

	public DragableImgLabel getImgLabel() {
		return imgLabel;
	}

	public void setImgLabel(DragableImgLabel imgLabel) {
		this.imgLabel = imgLabel;
	}
	
}
